
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Web.UI;
using Swaf;

using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet
{
    public class PopupActionButton : ActionButton, IPostBackEventHandler
    {
		protected static string s_popupClientScript = @"((window.showModalDialog('ActionPopup.aspx?C={2}&T={3}&I={4}',null,'dialogWidth:{0}; dialogHeight:{1}; center:yes; resizable: yes; help: no ')==true)?(__doPostBack('{5}', '') == 'X'):false)";
        protected string m_popupWidth = "350px";
        protected string m_popupHeight = "200px";
        protected string m_popupTitle = "Action Parameters";
        protected string m_popupControl = "";

        public override string ClientClick
        {
            get
            {
                return base.ClientClick;
            }
            set
            {
                base.ClientClick = value + string.Format(s_popupClientScript, m_popupWidth, m_popupHeight, m_popupControl, m_popupTitle, SessionId, UniqueID);
            }
        }

        protected string SessionId { get { return ID + "Popup"; } }
        protected PopupActionState ActionState
        {
            get
            {
                return Application.currentApp.sessionData[SessionId] as PopupActionState;
            }
            set
            {
                Application.currentApp.sessionData[SessionId] = value;
            }
        }

        public string PopupWidth { get { return m_popupWidth; } set { m_popupWidth = value; } }
        public string PopupHeight { get { return m_popupHeight; } set { m_popupHeight = value; } }
        public string ParamControlName { get { return m_popupControl; } set { m_popupControl = value; } }
        public string PopupTitle { get { return m_popupTitle; } set { m_popupTitle = value; } }

        protected override void OnPreRender(EventArgs e)
        {
            if (ClientClick == null)
                ClientClick = ""; //This causes the ClientClick setter to get called, which will add the popup window script to the empty string

            base.OnPreRender(e);
        }

        protected override void RaisePostBackEvent(string eventArgument)
        {
            PopupActionState state = ActionState;
			OnClick(state);
        }
    }
}
